<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Config;
use Dcat\Admin\Form;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;

class ConfigController extends AdminController
{

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Config(), function (Form $form) {
            $form->display('id');
            $form->column(5, function (Form $form) {
                $form->hidden('module')->default('admin');
                $form->select('group')->required()->options(\App\Models\Config::getGroupList());
                $form->text('name')->required();
                $form->text('title')->required();
                $form->textarea('value');
                $form->text('tip');
            });
            $form->column(7, function (Form $form) {
                $form->select('type')->when(['radio','checkbox','select','multipleSelect','listbox'], function (Form $form) {
                    $form->keyValue('content');
                })->when('list', function (Form $form) {
                    $form->list('content');
                })->when(['text'], function (Form $form) {
                    $form->textarea('content');
                })->required()->options(\App\Models\Config::getTypeList());
//            ->when('embeds', function () {
//                $this->embeds('content');
//            })->when('list', function () {
//                $this->list('content');
//            })
//            $this->text('content');
//                    $form->text('extend');
//                    $form->text('setting');
//            'keyValue'      => '键值组',
//            'embeds'        => '固定键值对象',
//            'list'          => '一维数组',
//            'table'         => '二维数组（table）',
//            'array'         => '二维数组（array）',
                $form->switch('required')->default(0);
                $form->textarea('rule');
            });
        });
    }

    public function index(Content $content)
    {
        // 标题和内容
        $card = Card::make("配置信息",new \App\Admin\Forms\Config\Config());
        // 设置间距
        $card->padding('0 15px 0 12px');
        if (env('APP_DEBUG')){
            // 设置工具按钮
            $addUrl = "general/config/create";
            $addClass = "btn btn-sm btn-primary shadow-none create-config";
            $addButton = '<button class="'.$addClass.'"><i class="fa fa-plus"></i>新增配置</button>';
            $card->tool($addButton);
            Form::dialog("新增配置")->click(".create-config")
                ->url($addUrl)->width('1200px')->success('Dcat.reload()');
        }
        // 设置底部内容
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($card);
    }
}
